package com.qijian.business.controller;

import java.util.Date;
import java.util.List;

import com.qijian.business.domain.query.ProductionDetailQuery;
import com.qijian.business.domain.vo.ProductionDetailSalaryVo;
import com.qijian.business.domain.vo.ProductionDetailVo;
import com.qijian.common.utils.DateUtils;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qijian.common.annotation.Log;
import com.qijian.common.annotation.PlugAnn;
import com.qijian.common.annotation.PlugParam;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.domain.entity.FlowableApproval;
import com.qijian.common.core.domain.entity.FlowableBack;
import com.qijian.common.enums.ApprovalDescriptionEnum;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.business.domain.ProductionDetailSalary;
import com.qijian.business.domain.query.ProductionDetailSalaryQuery;
import com.qijian.business.service.IProductionDetailSalaryService;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
/**
 * 报工工资Controller
 *
 * @author qijian
 * @date 2025-03-29
 */
@Api(tags = "报工工资")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/productionDetailSalary")
public class ProductionDetailSalaryController extends BaseController {

    private final IProductionDetailSalaryService productionDetailSalaryService;

    /**
     * 查询报工工资列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductionDetailSalaryQuery query, Page<ProductionDetailSalary> page) {
        initOrderItem(ProductionDetailSalary.class, page.orders());
        return getDataTable(productionDetailSalaryService.page(page, WhereEntityTool.invoke(query)));
    }

    /**
     * 导出报工工资列表
     */
    @Log(title = "报工工资" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(ProductionDetailSalaryQuery query, HttpServletResponse response) {
        new ExcelUtil<>(ProductionDetailSalary. class).exportExcel(response,productionDetailSalaryService.list( WhereEntityTool.invoke(query)), "报工工资数据");
    }

    /**
     * 获取报工工资详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(productionDetailSalaryService.getById(id));
    }

    /**
     * 新增报工工资
     */
    @Log(title = "报工工资" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionDetailSalary productionDetailSalary) {
        return toAjax(productionDetailSalaryService.save(productionDetailSalary));
    }

    /**
     * 修改报工工资
     */
    @Log(title = "报工工资" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionDetailSalary productionDetailSalary) {
        approval(productionDetailSalaryService.getById(productionDetailSalary.getId()));
        return toAjax(productionDetailSalaryService.updateById(productionDetailSalary));
    }

    /**
     * 删除报工工资
     */
    @Log(title = "报工工资" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        ids.forEach(id -> approval(productionDetailSalaryService.getById(id)));
        return toAjax(productionDetailSalaryService.removeByIds(ids));
    }

    /**
     * 获取班组报工列表
     * @param productionDetailSalaryQuery
     * @return
     */
    @GetMapping("/getTeamProduction")
    public TableDataInfo teamProduction(ProductionDetailSalaryQuery productionDetailSalaryQuery, Page<ProductionDetailSalary> page) {
        Page<ProductionDetailSalaryVo> list = productionDetailSalaryService.getTeamProduction(productionDetailSalaryQuery, page);
        return getDataTable(list);
    }

    /**
     * 班组收益
     * @param productionDetailSalaryQuery
     * @return
     */
    @GetMapping("/getTeamSalary")
    public AjaxResult getTeamSalary(ProductionDetailSalaryQuery productionDetailSalaryQuery) {
        List<ProductionDetailSalaryVo> productionDetailSalaryVo = productionDetailSalaryService.getTeamSalary(productionDetailSalaryQuery);
        return AjaxResult.data(productionDetailSalaryVo);
    }


    /**
     * 班组长确认报工
     * @param
     * @return
     */
    @PostMapping("/checkProduction")
    public AjaxResult checkProduction(@RequestBody List<ProductionDetailSalary> productionDetailSalaryList) {
        for (ProductionDetailSalary productionDetailSalary : productionDetailSalaryList) {
            ProductionDetailSalary byId = productionDetailSalaryService.getById(productionDetailSalary.getId());
            if (StringUtils.isNull(byId)) {
                throw new ServiceException("报工记录不存在");
            }
            if (byId.getTeamLeaderCheck()) {
                throw new ServiceException("报工记录已确认");
            }
            productionDetailSalary.setTeamLeaderCheck(true);
            productionDetailSalary.setCheckTime(new Date());
            productionDetailSalary.setCheckUserId(SecurityUtils.getUserId());
            productionDetailSalary.setCheckUsername(SecurityUtils.getUsername());
            if (StringUtils.isNull(productionDetailSalary.getCheckQty())) {
                productionDetailSalary.setCheckQty(productionDetailSalary.getFinishQty());
            }
        }

        return toAjax(productionDetailSalaryService.updateBatchById(productionDetailSalaryList));
    }
}
